﻿@model Sheng.WeixinConstruction.Management.Shell.Models.MemberInfoViewModel

@{
    ViewBag.Title = "MemberInfo";
    Layout = "~/Views/Shared/_LayoutBlank.cshtml";
}

<script type="text/javascript">

    var _id = getQueryString("id");

    var _pointChanged = false;

    var _divPointEditHtml;
    var _divCashEditHtml;
    var _setMemberCardHtml;
    var _adjustmentType = "increment"; // decrement



    $(document).ready(function () {

        _divPointEditHtml = $("#divPointEdit").html();
        $("#divPointEdit").html("");

        _divCashEditHtml = $("#divCashEdit").html();
        $("#divCashEdit").html("");

        _setMemberCardHtml = $("#divSetMemberCard").html();
        $("#divSetMemberCard").html("");

        $("[keyenter]").keypress(function (e) {
            if (e.keyCode == 13) {
                save();
            }
        });

    });

    function showPointIncrement() {
        layer.open({
            type: 1,
            area: ['350px', '240px'], //宽高
            closeBtn: false,
            title: "",
            shift: _layerShift,
            content: _divPointEditHtml,
            success: function (layero, index) {
                _adjustmentType = "increment";
                $("#spanInputFormTitle").html("增加积分");
                $(layero).focus();
            }
        });
    }

    function showPointDecrement() {
        layer.open({
            type: 1,
            area: ['350px', '240px'], //宽高
            closeBtn: false,
            title: "",
            shift: _layerShift,
            content: _divPointEditHtml,
            success: function (layero, index) {
                _adjustmentType = "decrement";
                $("#spanInputFormTitle").html("消减积分");
                $(layero).focus();
            }
        });
    }

    function pointAdjustment() {
        var txtChangePointValidationMessage = "请输入要增加的积分额度；";

        if (_adjustmentType == "decrement") {
            txtChangePointValidationMessage = "请输入要消减的积分额度；";
        }

        if ($("#formPointEdit").validate({
            onfocusout: false,
            onkeyup: false,
            showErrors: showValidationErrors,
            rules: {
                "txtChangePoint": {
            required: true,
            number: true,
            digits: true,
            range: [1, 10000]
        }
        },
            messages: {
                "txtChangePoint": {
            required: txtChangePointValidationMessage,
            number: "请输入有效整数字；",
            digits: "请输入有效整数字；",
            range: "请介于 1 到 10000 之间；"
        }
        }
        }).form() == false) {
            return;
        }

        var quantity = $("#txtChangePoint").val();
        if (_adjustmentType == "decrement") {
            quantity = 0 - quantity;
        }
        var remark = $("#txtChangePointRemark").val();

        closeAllLayer();

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        var args = new Object();
        args.MemberId = _id;
        args.Quantity = quantity;
        args.Remark = remark;

        var url = "/Api/Member/PointIncrement";

        if (_adjustmentType == "decrement") {
            url = "/Api/Member/PointDecrement";
        }

        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            data: JSON.stringify(args),
            success: function (data, status, jqXHR) {
                layer.close(loadLayerIndex);
                if (data.Success) {
                    _pointChanged = true;
                    $("#spanPoint").html(data.Data.LeftPoint);
                    layerAlert("该会员的积分已调整到：" + data.Data.LeftPoint);
                } else {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest) {
                layer.close(loadLayerIndex);
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function showCashIncrement() {
        layer.open({
            type: 1,
            area: ['350px', '240px'], //宽高
            closeBtn: false,
            title: "",
            shift: _layerShift,
            content: _divCashEditHtml,
            success: function (layero, index) {
                _adjustmentType = "increment";
                $("#spanInputFormTitle").html("现金充值");
                $(layero).focus();
            }
        });
    }

    function showCashDecrement() {
        layer.open({
            type: 1,
            area: ['350px', '240px'], //宽高
            closeBtn: false,
            title: "",
            shift: _layerShift,
            content: _divCashEditHtml,
            success: function (layero, index) {
                _adjustmentType = "decrement";
                $("#spanInputFormTitle").html("现金消费");
                $(layero).focus();
            }
        });
    }

    function cashAdjustment() {
        var txtChangePointValidationMessage = "请输入充值金额；";

        if (_adjustmentType == "decrement") {
            txtChangePointValidationMessage = "请输入消费金额；";
        }

        if ($("#formCashEdit").validate({
            onfocusout: false,
            onkeyup: false,
            showErrors: showValidationErrors,
            rules: {
                "txtChangeCash": {
            required: true,
            number: true,
            range: [0.01, 1000000],
            cash: true,
        }
        },
            messages: {
                "txtChangeCash": {
            required: txtChangePointValidationMessage,
            number: "金额请输入有效数字；",
            range: "金额请介于 0.01 到 1000000 之间；"
        }
        }
        }).form() == false) {
            return;
        }

        var quantity = $("#txtChangeCash").val();
        if (_adjustmentType == "decrement") {
            quantity = 0 - quantity;
        }
        var remark = $("#txtChangeCashRemark").val();

        closeAllLayer();

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        var args = new Object();
        args.MemberId = _id;
        args.Fee = quantity;
        args.Remark = remark;

        var url = "/Api/Pay/CashDeposit";

        if (_adjustmentType == "decrement") {
            url = "/Api/Pay/Charge";
        }

        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            data: JSON.stringify(args),
            success: function (data, status, jqXHR) {
                layer.close(loadLayerIndex);
                if (data.Success) {
                    switch (data.Data.Reason) {
                        case 0:
                            _pointChanged = true;
                            $("#spanCashAccount").html(data.Data.LeftCashAccount / 100.0);
                            layerAlert("成功，该会员当前现金余额：" + data.Data.LeftCashAccount / 100.0);
                            break;
                        case 1:
                            layerAlert("失败，余额不足。");
                            break;
                        case 2:
                            layerAlert("失败，指定的会员不存在。");
                            break;
                        case 3:
                        case 4:
                            layerAlert("失败，无权限。");
                            break;
                        default:
                            layerAlert("未知结果：" + data.Data.Reason);
                            break;
                    }

                } else {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest) {
                layer.close(loadLayerIndex);
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function showSetMemberCard() {
        layer.open({
            type: 1,
            area: ['350px', '175px'], //宽高
            closeBtn: false,
            title: "",
            shift: _layerShift,
            content: _setMemberCardHtml,
            success: function (layero, index) {
                $(layero).focus();
            }
        });
    }

    function setMemberCard() {

        if ($("#formSetMemberCard").validate({
            onfocusout: false,
            onkeyup: false,
            showErrors: showValidationErrors,
            rules: {
                "selectDefaultMemberCardLevel": "required"
        },
            messages: {
                "selectDefaultMemberCardLevel": "请选择会员卡，您可在“设置”菜单下的“会员卡级别”中配置您的会员卡。"
        }
        }).form() == false) {
            return;
        }

        var memberCardId = $("#selectDefaultMemberCardLevel").val();

        closeAllLayer();

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        var args = new Object();
        args.MemberId = $("#txtId").val();
        args.MemberCardId = memberCardId;

        var url = "/Api/Member/SetMemberLevel";

        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            data: JSON.stringify(args),
            success: function (data, status, jqXHR) {
                layer.close(loadLayerIndex);
                if (data.Success) {
                    layerAlert("设置成功！", refresh);
                } else {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest) {
                layer.close(loadLayerIndex);
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function refresh() {
        location.reload();
    }

    function cancel() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

        if (_pointChanged) {
            parent.loadDataOnPageAndCloseLayer(index);
        }
        else {
            parent.layer.close(index); //再执行关闭
        }
    }

    function closeAllLayer() {
        layer.closeAll();
    }

</script>

<script id="divPointEdit" type="text/html">
    <div style="margin-left:10px; margin-right:10px;margin-top:20px; margin-bottom:10px;">
        <div style="width:100%;">
            <div>
                <span id="spanInputFormTitle" class="font_blue_20"></span>
            </div>
            <div style="width:100%; margin-left:0px; margin-top:20px;">
                <form id="formPointEdit">
                    <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="70">额度：</td>
                            <td height="36">
                                <input id="txtChangePoint" name="txtChangePoint" type="text" class="input_16" style="width:96%; " keyenter=keyenter />
                            </td>
                        </tr>
                        <tr>
                            <td width="70">备注：</td>
                            <td height="36">
                                <input id="txtChangePointRemark" name="txtChangePointRemark" type="text" class="input_16" style="width:96%; " maxlength="50" keyenter=keyenter />
                            </td>
                        </tr>
                        <tr>
                            <td width="70">&nbsp;</td>
                            <td height="36">
                                <div class="font_gray_13" style="width:96%;margin-bottom:10px;">
                                    粉丝可见。
                                </div>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <div style=" width:100%; margin-top:20px; margin-left:0px;">
                <div style="float:right">
                    <input name="btnSave" type="button" class="btn_blue" id="btnSave" value="确 定" onclick="pointAdjustment()" />
                    <input name="btnCancel" type="button" class="btn_blue" id="btnCancel" value="取 消" onclick="closeAllLayer()" />
                </div>
                <div style="clear:both">
                </div>
            </div>
        </div>
    </div>
</script>

<script id="divCashEdit" type="text/html">
    <div style="margin-left:10px; margin-right:10px;margin-top:20px; margin-bottom:10px;">
        <div style="width:100%;">
            <div>
                <span id="spanInputFormTitle" class="font_blue_20"></span>
            </div>
            <div style="width:100%; margin-left:0px; margin-top:20px;">
                <form id="formCashEdit">
                    <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
                        <tr>
                            <td width="70">金额：</td>
                            <td height="36">
                                <input id="txtChangeCash" name="txtChangeCash" type="text" class="input_16" style="width:96%; " keyenter=keyenter />
                            </td>
                        </tr>
                        <tr>
                            <td width="70">备注：</td>
                            <td height="36">
                                <input id="txtChangeCashRemark" name="txtChangeCashRemark" type="text" class="input_16" style="width:96%; " maxlength="50" keyenter=keyenter />
                            </td>
                        </tr>
                        <tr>
                            <td width="70">&nbsp;</td>
                            <td height="36">
                                <div class="font_gray_13" style="width:96%;margin-bottom:10px;">
                                    粉丝可见。
                                </div>
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <div style=" width:100%; margin-top:20px; margin-left:0px;">
                <div style="float:right">
                    <input name="btnSave" type="button" class="btn_blue" id="btnSave" value="确 定" onclick="cashAdjustment()" />
                    <input name="btnCancel" type="button" class="btn_blue" id="btnCancel" value="取 消" onclick="closeAllLayer()" />
                </div>
                <div style="clear:both">
                </div>
            </div>
        </div>
    </div>
</script>

<script id="divSetMemberCard" type="text/html">
    <div style="margin-left:10px; margin-right:10px;margin-top:20px; margin-bottom:10px;">
        <div style="width:100%;">
            <div>
                <span id="divPointEditTitle" class="font_blue_20">设置会员卡</span>
            </div>
            <div style="width:100%; margin-left:0px; margin-top:20px;">
                <form id="formSetMemberCard">
                    <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
                        <tr>
                            <td height="36">
                                @if (Model.MemberCardLevelList.Count > 0)
                                {
                                    <select id="selectDefaultMemberCardLevel" name="selectDefaultMemberCardLevel" class="input_16" keyenter=keyenter>
                                        @foreach (var item in Model.MemberCardLevelList)
                                        {
                                            <option value="@item.Id" @if (Model.MemberCard != null && item.Id == Model.MemberCard.Id) { @Html.Raw("selected")      }>@item.Name</option>
                                        }
                                    </select>
                                }
                                else
                                {
                                    <span>没有可用的会员卡，您可在“设置”菜单下的“会员卡级别”中配置您的会员卡。</span>
                                }
                            </td>
                        </tr>
                    </table>
                </form>
            </div>
            <div style=" width:100%; margin-top:20px; margin-left:0px;">
                <div style="float:right">
                    @if (Model.MemberCardLevelList.Count > 0)
                    {
                        <input name="btnSave" type="button" class="btn_blue" id="btnSave" value="确 定" onclick="setMemberCard()" />
                    }
                    <input name="btnCancel" type="button" class="btn_blue" id="btnCancel" value="取 消" onclick="closeAllLayer()" />
                </div>
                <div style="clear:both">
                </div>
            </div>
        </div>
    </div>
</script>


<div style="margin-left:20px; margin-right:20px; margin-top:20px;">
    <span id="spanTitle" class="font_black_24">会员信息</span>
</div>

<div style=" background-color:#ccc; margin-left:20px; margin-right:20px; margin-top:10px; height:1px;">

</div>

<div style=" position:absolute; overflow:auto ;margin-top:25px;left:30px; right:30px; bottom:60px; top:50px; ">
    <div style="background-color:lightgray;width:132px;height:132px;float:right">
        <img src="@Model.Member.Headimgurl_132" />
    </div>
    <div style="float:left">
        <input type="hidden" id="txtId" value="@Model.Member.Id" />
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="110" height="36">微信昵称：</td>
                <td>@Model.Member.NickName</td>
            </tr>
            <tr>
                <td width="110" height="36">会员卡级别：</td>
                <td>
                    @if (Model.MemberCard != null)
                    {
                        <span>@Model.MemberCard.Name</span>
                    }
                    <span style="margin-left:10px;">
                        <input name="btnSetMemberCard" type="button" class="btn_orange_small" id="btnSetMemberCard" value="设置" onclick="showSetMemberCard()" />
                    </span>
                </td>
            </tr>
            <tr>
                <td width="110" height="36">会员卡号：</td>
                <td>@Model.Member.CardNumber</td>
            </tr>
            <tr>
                <td width="110" height="36">姓名：</td>
                <td>@Model.Member.Name</td>
            </tr>
            <tr>
                <td width="110" height="36">性别：</td>
                <td>@Model.Member.SexString</td>
            </tr>
            <tr>
                <td width="110" height="36">积分：</td>
                <td>
                    <span id="spanPoint">@Model.Member.Point</span>
                    <span style="margin-left:10px;">
                        <input name="btnIncrement" type="button" class="btn_orange_small" id="btnIncrement" value="增加" onclick="showPointIncrement()" />
                        <input name="btnDecrement" type="button" class="btn_orange_small" id="btnDecrement" value="消减" onclick="showPointDecrement()" />
                    </span>
                </td>
            </tr>
            <tr>
                <td width="110" height="36">现金余额：</td>
                <td>
                    <span id="spanCashAccount">@(Model.Member.CashAccount / 100f)</span>
                    <span style="margin-left:10px;">
                        <input name="btnCashDeposit" type="button" class="btn_orange_small" id="btnCashDeposit" value="增加" onclick="showCashIncrement()" />
                        <input name="btnCashCharge" type="button" class="btn_orange_small" id="btnCashCharge" value="消减" onclick="showCashDecrement()" />
                    </span>
                </td>
            </tr>
            <tr>
                <td width="110" height="36">城市：</td>
                <td>@Model.Member.Country @Model.Member.Province @Model.Member.City</td>
            </tr>
            <tr>
                <td width="110" height="36">手机号码：</td>
                <td>@Model.Member.MobilePhone</td>
            </tr>
            <tr>
                <td width="110" height="36">电子邮箱：</td>
                <td>@Model.Member.Email</td>
            </tr>
            <tr>
                <td width="110" height="36">关注时间：</td>
                <td>
                    @if (Model.Member.Attention)
                    {
                        @Model.Member.SubscribeTime
                    }
                    else
                    {
                        @:已取消关注
                    }
                </td>
            </tr>
            <tr>
                <td width="110" height="36">OpenId：</td>
                <td>
                    @if (ViewBag.Domain.Type == Sheng.WeixinConstruction.Infrastructure.EnumDomainType.Free)
                    {
                        @Helpers.VipIcon()
                    }
                    else
                    {
                        @Model.Member.OpenId
                    }
                </td>
            </tr>
        </table>
    </div>
    <div style="clear:both"></div>
</div>

<div style=" background-color:#ccc; position:absolute; bottom:55px; left:20px;right:20px; height:1px;">

</div>

<div style="position:absolute; bottom:15px; left:20px;right:20px;">


    <div class="font_black_14" style="float:left;">
        欲将会员设置为管理员，请至“系统”中的“用户管理”进行设置。
    </div>

    <div style="float:right">
        <!--<input name="btnSave" type="button" class="btn_blue" id="btnSave" value="保 存" />-->
        <input name="btnCancel" type="button" class="btn_blue" id="btnCancel" value="关 闭" onclick="cancel()" />
    </div>
    <div style="clear:both">
    </div>
</div>
